package org.csu.small.controller.front;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.csu.small.common.Constant;
import org.csu.small.common.ResponseCode;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.User;
import org.csu.small.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author Rambo
 * @date 2020-06-08 9:02
 */
@Controller
@RequestMapping("/user/")
@Api(value = "API - UserController", description = "用户模块接口详情")
public class UserController {

    @Autowired
    private IUserService userService;


    @PostMapping("login")
    @ResponseBody
    @ApiOperation(value = "登录")
    public UniformResponse<User>  login(String loginString, String password, HttpSession session){
        UniformResponse<User> response = userService.login(loginString,password);
        if(response.isSuccess()){
            session.setAttribute(Constant.LOGIN_USER, response.getData());
        }
        //生成令牌，用Guawacache
        return response;
    }

    @PostMapping("logout")
    @ResponseBody
    public UniformResponse<String>  logout(HttpSession session){
        session.invalidate();
        return UniformResponse.createForSuccess("退出成功");
    }

    @GetMapping("get_user_info")
    @ResponseBody
    public UniformResponse<User> getUserInfo(HttpSession session){

        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user != null){
            return UniformResponse.createForSuccess(user);
        }
        return UniformResponse.createForErrorMessage("用户未登录");
    }

    @GetMapping("get_infomation")
    @ResponseBody
    public UniformResponse<User> getInfomation(HttpSession session){

        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user != null){
            return UniformResponse.createForSuccess(user);
        }
        return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
    }

    @GetMapping("update_information")
    @ResponseBody
    public UniformResponse<String> updateInfomation(String username, String email, HttpSession session){

        if (isLogin(session)) {

            User user = (User) session.getAttribute(Constant.LOGIN_USER);
            UniformResponse<String> uniformResponse = userService.updateInfomation(user, username, email);
            if(uniformResponse.isSuccess()){
                user.setUsername(username);
                user.setEmail(email);
                session.setAttribute(Constant.LOGIN_USER, user);
            }
            return uniformResponse;
        }
        return UniformResponse.createForErrorMessage("用户未登录");
    }

    @PostMapping("register")
    @ResponseBody
    public UniformResponse<String> register(User user){
        return userService.register(user);
    }

    @PostMapping("check_valid")
    @ResponseBody
    public UniformResponse<String> checkField(String type, String value){
        return userService.checkField(type,value);
    }

    @PostMapping("check_loginString")
    @ResponseBody
    public UniformResponse<String> checkLoginString(String value, String type){
        return userService.checkLoginString(value,type);
    }

    @PostMapping("send_code")
    @ResponseBody
    public UniformResponse<String> sendCode(String phone){
        return userService.sendCode(phone);
    }

    @PostMapping("check_phone_and_code")
    @ResponseBody
    public UniformResponse<String> checkPhoneAndCode(String phone, String code){
        return userService.checkPhoneAndCode(phone,code);
    }

    @PostMapping("reset_password_by_phone")
    @ResponseBody
    public UniformResponse<String> resetPasswordByPhone(String phone,String passwordNew){
        return userService.resetPasswordByPhone(phone,passwordNew);
    }

    @PostMapping("reset_password")
    @ResponseBody
    public UniformResponse<String> resetPassword(String passwordOld, String passwordNew, HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user != null){
            return userService.resetPassword(user, passwordOld, passwordNew);
        }
        return UniformResponse.createForErrorMessage("用户未登录,无法获取当前用户信息");
    }

    public boolean isLogin(HttpSession session){

        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        return user != null;
    }

}
